package com.dldata.devtools30.controller.api;


import com.dldata.devtools30.entity.DevtApiMainEntity;
import com.dldata.devtools30.entity.DevtApiSubEntity;
import com.dldata.devtools30.service.api.DevtApiMainEntityService;
import com.dldata.devtools30.service.api.DevtApiSubEntityService;
import com.dldata.devtools30.utils.Public;
import com.dldata.devtools30.utils.StringUtil;
import com.dldata.devtools30.utils.Util;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping(value = "/V_DevtApiSubEntityController")
public class V_DevtApiSubEntityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DevtApiMainEntityService devtApiMainEntityService;
    @Autowired
    DevtApiSubEntityService devtApiSubEntityService;

    @RequestMapping(value = "/optPage")
    public String toOptPage(String id,ModelMap model){
        model.addAttribute("SQLSPLIT", Public.SQLSPLIT);
        return "thymeleaf/devtools30/api/v_DevtApiSubOptEdit";
    }
    
    @RequestMapping(value = "/optPageJson")
    public String optPageJson(String id,ModelMap model){
        model.addAttribute("SQLSPLIT", Public.SQLSPLIT);
        return "thymeleaf/devtools30/api/v_DevtApiSubOptEditJson";
    }

    @RequestMapping(value = "/edit")
    public String edit(String id,HttpServletRequest request,
                           HttpServletResponse response,
                           ModelMap model
    ) {

        model.addAttribute("frtlist", Util.getEnumMapNative(DevtApiMainEntityService.FunctionResultType.class));

        model.addAttribute("ftlist", Util.getEnumMapNative(DevtApiMainEntityService.FunctionType.class));

        model.addAttribute("SQLSPLIT", Public.SQLSPLIT);

        List<DevtApiMainEntity> list= devtApiMainEntityService.getAllData();
        List mainList = new LinkedList();
        for (int i = 0; i < list.size(); i++) {
            DevtApiMainEntity o =  list.get(i);
            HashMap map = new HashMap();
            map.put("code",o.getId());
            map.put("label",o.getMc());
            mainList.add(map);
        }
        model.addAttribute("mainList", mainList);
    
        DevtApiSubEntity entity = new DevtApiSubEntity();
        entity.setMaxcachetime(0);
        entity.setLx(StringUtil.isEmptyOrLength0(request.getParameter("apisubtype"))?5:Integer.valueOf(request.getParameter("apisubtype")));
        if(!StringUtil.isNullOrSpace(id)){
            List<DevtApiSubEntity> entities = devtApiSubEntityService.getOneData(id);
            if(entities.size()>0){
                entity=entities.get(0);
            }
        }

        model.addAttribute("data",entity);

        return "thymeleaf/devtools30/api/v_DevtApiSubEdit";
    }

}
